package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.util.DateUtils;
import com.itheima.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        Member member = memberDao.findMemberByPhoneNumber ( telephone );
        return member;
    }

    @Override
    public void add(Member member) {
        if (member.getPassword () != null) {
            //对会员密码进行加密
            String password = MD5Utils.md5 ( member.getPassword () );
            member.setPassword ( password );
        }
        memberDao.add ( member );
    }

    //查询过去一年每个月会员注册数量
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> list = new ArrayList<> ();
        for (String month : months) {
            month = month + ".31";
            Integer count = memberDao.findMemberCountByMonths ( month );
            list.add ( count );
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> findMemberCountBySex() {
        List<Map<String, Object>> memberCountBySex = memberDao.findMemberCountBySex ();
        System.out.println ( memberCountBySex.get ( 0 ) );
        System.out.println ( memberCountBySex.get ( 1 ) );
        for (Map<String, Object> countBySex : memberCountBySex) {
            String memberSex = (String) countBySex.get ( "name" );

            if (memberSex.equals ( "1" )) {
                memberSex = "男";
                countBySex.put ( "name", memberSex );
            } else {
                memberSex = "女";
                countBySex.put ( "name", memberSex );
            }
        }
        return memberCountBySex;
    }

    @Override
    public List<Map<String, Object>> findMemberCountByYears(List<Map<String, String>> ages) throws Exception {
        List<Map<String, Object>> list1 =new ArrayList<> (  );
        for (Map<String, String> age : ages) {
            String now = age.get ( "now" );
            String before = age.get ( "before" );
           /* System.out.println (now);
            System.out.println (before);*/
            List<Map<String, Object>> list = memberDao.findMemberCountByYears ( now, before );
            for (Map<String, Object> stringObjectMap : list) {
                DateFormat dateFormat = new SimpleDateFormat ( "yyyy-MM-dd" );
                Date time = (Date) stringObjectMap.get ( "name" );
                String s1 = DateUtils.parseDate2String ( time );
                Date today = DateUtils.getToday();
                String s = DateUtils.parseDate2String ( today );
                long time1 = dateFormat.parse ( s ).getTime ();
                long time2 = dateFormat.parse ( s1 ).getTime ();
                long time4 = (time1 - time2);
                double time3 = time4 * 1.0 / 1000 / 60 / 60 / 24 / 365;
                if (time3 <= 18 && time3 >= 0) {
                    stringObjectMap.put ( "name", "0-18岁" );

                } else if (time3 > 18 && time3 <= 30) {
                    stringObjectMap.put ( "name", "18-30岁" );
                } else if (time3 > 30 && time3 <= 45) {
                    stringObjectMap.put ( "name", "30-45岁" );
                } else if(time3 >45) {
                    stringObjectMap.put ( "name", "45岁以上" );
                }else{
                    return null;
                }

                list1.add ( stringObjectMap );
            }

        }
        return list1;

    }

    @Override
    public List<Map<String, Object>> findMemberCountByAges() {
        List<Map<String, Object>> memberCountByAges = memberDao.findMemberCountByAges ();
        return memberCountByAges;
    }

    /*@Override
    public List<Map<String, Object>> findMemberCountByAges() {
        List<Map<String, Object>> memberCountByAges = memberDao.findMemberCountByAges ();
        int sum1=0;
        int sum2=0;
        int sum3=0;
        int sum4=0;
        List<Map<String, Object>>  map=new ArrayList<> (  );
        Map map1=new HashMap (  );
        Map map2=new HashMap (  );
        Map map3=new HashMap (  );
        Map map4=new HashMap (  );
        for (Map<String, Object> memberCountByAge : memberCountByAges) {
            Double time3 = (Double) memberCountByAge.get ( "name" );
            Long value = (Long) memberCountByAge.get ( "value" );
            if (time3 <= 18 && time3 >= 0) {
                sum1+=value;
            } else if (time3 > 18 && time3 <= 30) {
                sum2+=value;
            } else if (time3 > 30 && time3 <= 45) {
                sum3+=value;
            } else if(time3 >45) {
                sum4+=value;
            }
        }
        map1.put ( "name", "0-18岁" );
        map1.put ( "value",sum1 );

        map2.put ( "name", "18-30岁" );
        map2.put ( "value",sum2 );

        map3.put ( "name", "30-45岁" );
        map3.put ( "value",sum3 );
        map4.put ("name", "45岁以上"  );
        map4.put ( "value",sum4 );

        map.add ( map1 );
        map.add ( map2 );
        map.add ( map3 );
        map.add ( map4 );
        return map;
    }*/

}
